﻿using Inovout.Repositories;
using Inovout.Security.Models;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Security.Repositories
{
    public class UserCredentialRepository : DataObjectRepository<UserCredential>
    {
        internal UserCredential FindByUserNameIdentifierCode(string userName)
        {
            FormsUserCredential formsUserCredential = ComponentRegistry.Resolve<IRepository<FormsUserCredential>>().Find(Restrictions.Eq("UserName", userName));
            if (formsUserCredential != null)
            {
                return formsUserCredential;
            }

            OpenIdUserCredential openIdUserCredential = ComponentRegistry.Resolve<IRepository<OpenIdUserCredential>>().Find(Restrictions.Eq("Identifier", userName));
            if (openIdUserCredential != null)
            {
                return openIdUserCredential;
            }

            OAuthUserCredential oauthUserCredential = ComponentRegistry.Resolve<IRepository<OAuthUserCredential>>().Find(Restrictions.Eq("Code", userName));
            if (oauthUserCredential != null)
            {
                return oauthUserCredential;
            }

            return null;
        }
    }
}
